<?php
namespace User\Controller;

use Common\Controller\HomebaseController;

class IndexController extends HomebaseController {

    function _initialize() {
        parent::check_login();
    }
    
    // 前台用户首页 (公开)
	public function index() {
        $users_model=M("Users");
        $order_model = M('Order');
        $user = $users_model->where(array('id'=>session('user')['id']))->find();
        $user['order_num'] = $order_model->where(array('user_id'=>$user['id'],'order_type'=>array('lt'=>4)))->count();
		$this->assign('user',$user);
		$this->display(":index");
    }

    public function userinfo(){
        $users_model=M("Users");
        if(IS_POST){
            $rules = array(
                //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
                array('mobile', '/^(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$/', '手机号有误！', 1, 'regex', 3),
            );
            if($users_model->validate($rules)->create()===false){
                $this->error($users_model->getError());
            }
            $id = I('id');
            $data = $_POST;
            $re = $users_model->save($data);
            if($re){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }

        }else{
            $user = $users_model->where(array('id'=>session('user')['id']))->find();
            $this->assign('user',$user);
            $this->display(":userinfo");
        }
    }


    
    // 前台ajax 判断用户登录状态接口
    function is_login(){
    	if(sp_is_user_login()){
    		$this->ajaxReturn(array("status"=>1,"user"=>sp_get_current_user()));
    	}else{
    		$this->ajaxReturn(array("status"=>0,"info"=>"此用户未登录！"));
    	}
    }

    //退出
    public function logout(){
    	$ucenter_syn=C("UCENTER_ENABLED");
    	$login_success=false;
    	if($ucenter_syn){
    		include UC_CLIENT_ROOT."client.php";
    		echo uc_user_synlogout();
    	}
    	session("user",null);//只有前台用户退出
    	redirect(__ROOT__."/");
    }

}
